package com.meepo.basic.auth

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.meepo.basic.API
import com.meepo.basic.R
import com.meepo.basic.schema.user.User
import com.meepo.basic.components.view.MeepoActivity
import com.meepo.framework.design.helper.StatusBarHelper
import com.umeng.analytics.MobclickAgent
import kotlinx.android.synthetic.main.login_code_activity.*
import kotlinx.android.synthetic.main.login_code_activity.close_iv
import kotlinx.android.synthetic.main.login_code_activity.header_rl

class LoginCodeActivity : MeepoActivity() {


    private var countDownTimer: CountDownTimer? = null


    private fun onLoginSuccess(user: User) {

        val intent = Intent()
        intent.putExtra("userSummary", user)
        setResult(0, intent)

        MobclickAgent.onProfileSignIn("code", user.id)

        finish()
    }

    override fun initData() {
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {
        StatusBarHelper.immerse(this, header_rl, true)

        val phoneNo = intent.getStringExtra("phoneNo")!!

        close_iv.setOnClickListener { finish() }
//        switchLoginButton(false)


        code_et.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {}

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                switchLoginButton(!p0.isNullOrEmpty() && p0.length == 4)
            }
        })

        phone_tv.text = phoneNo

        resend_tv.setOnClickListener {
            presenter.call(
                API.get().sendCode(phoneNo),
                {}
            )
        }

        countDownTimer = object : CountDownTimer(1000*60, 1000) {

            @SuppressLint("SetTextI18n")
            override fun onTick(millisUntilFinished: Long) {
                resend_tv.setTextColor(resources.getColor(R.color.hint_text_color))
                resend_tv.isEnabled = false
                resend_tv.text = "重新发送(${millisUntilFinished/1000})"
            }

            override fun onFinish() {
                resend_tv.text = "重新发送"
                resend_tv.setTextColor(resources.getColor(R.color.sub_text_color))
                resend_tv.isEnabled = true
            }

        }
        countDownTimer?.start()
//
//        Countdown(RESEND_CODE_DURATION, object : OnChangeListener {
//            override fun onComplete() {
//                resend_tv.text = "重新发送"
//                resend_tv.setTextColor(resources.getColor(R.color.sub_text_color))
//                resend_tv.isEnabled = true
//            }
//
//            @SuppressLint("SetTextI18n")
//            override fun onNext(p0: Long?) {
//                resend_tv.text = "重新发送(${p0})"
//            }
//
//            override fun onError(p0: Throwable?) {
//            }
//
//            @SuppressLint("SetTextI18n")
//            override fun onStart() {
//                resend_tv.setTextColor(resources.getColor(R.color.hint_text_color))
//                resend_tv.isEnabled = false
//                resend_tv.text = "重新发送(${RESEND_CODE_DURATION})"
//            }
//        }).start()

        login_btn.setOnClickListener {
            presenter.call(
                API.get().loginByCode(phoneNo, code_et.text.toString()),
                { rsp -> rsp.data?.let { onLoginSuccess(it) } }
            )
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        countDownTimer?.cancel()
    }


    private fun switchLoginButton(isEnabled: Boolean) {
        login_btn.isEnabled = isEnabled
        login_btn.alpha = if (isEnabled) 1.0f else 0.5f
    }

    override fun setUpContentLayout(): Int = R.layout.login_code_activity
}